// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.

#include "StdAfx.h"
#include "QvXmlDocument.h"
#include "QvXmlNodeList.h"
#include "QvXmlNode.h"

QvXmlNodeList::QvXmlNodeList(IXMLDOMNodeList* pChildNodes) : QvObject()
{
    //
    // Store the pointer and take a reference
    //
    m_pChildNodes = pChildNodes;
    m_pChildNodes->AddRef();
}

QvXmlNodeList::QvXmlNodeList(QvXmlNodeList const & obj)
{
    m_pChildNodes = obj.m_pChildNodes;
    if(m_pChildNodes)
    {
        m_pChildNodes->AddRef();
    }
}

QvXmlNodeList& QvXmlNodeList::operator=(QvXmlNodeList const & obj)
{
    if(m_pChildNodes)
    {
        m_pChildNodes->Release();
    }
    m_pChildNodes = obj.m_pChildNodes;
    if(m_pChildNodes)
    {
        m_pChildNodes->AddRef();
    }
    return *this;
}

QvXmlNodeList::~QvXmlNodeList()
{
    if (NULL != m_pChildNodes)
    {
        m_pChildNodes->Release();
        m_pChildNodes = NULL;
    }
}

int QvXmlNodeList::Count()
{
    long nNodes = 0;
    m_pChildNodes->get_length(&nNodes);
    return nNodes;
}

QvXmlNode* QvXmlNodeList::GetNode(long index)
{
    HRESULT hr = S_OK;
    QvXmlNode *pXmlNode = NULL;
    IXMLDOMNode *pNode = NULL;

    hr = m_pChildNodes->get_item(index, &pNode);
    if (SUCCEEDED(hr))
    {
        pXmlNode = new QvXmlNode(pNode);
        //
        // Release the DOM Node
        //
        pNode->Release();
        pNode = NULL;
    }

    return pXmlNode;
}

